package com.yaoweigroup.community.adapter;

import java.util.List;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Vibrator;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.exception.DbException;
import com.yaoweigroup.community.R;
import com.yaoweigroup.community.db.DBUtil;
import com.yaoweigroup.community.dialog.DialogCallBack;
import com.yaoweigroup.community.enity.HomeIcon;
import com.yaoweigroup.community.enity.ShopType;
import com.yaoweigroup.community.util.ApkDownloadUtil;
import com.yaoweigroup.community.util.BitmapHelp;
import com.yaoweigroup.community.util.DialogUtil;
import com.yaoweigroup.community.util.LogUtil;
import com.yaoweigroup.community.util.Utils;

public class ShopGridAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private List<ShopType> items;
	private Context context;
	private DBUtil dbUtil;
	private int selectPosition = -1;
	private Handler handler = new Handler();
	private BitmapUtils bitmapUtils;
	private BitmapDisplayConfig bigPicDisplayConfig;

	public ShopGridAdapter(Context context, List<ShopType> items) {
		this.items = items;
		inflater = LayoutInflater.from(context);
		this.context = context;
		dbUtil = DBUtil.newInstance(context);
		bitmapUtils = BitmapHelp.getBitmapUtils(context);
		bigPicDisplayConfig = new BitmapDisplayConfig();
		bigPicDisplayConfig.setLoadFailedDrawable(context.getResources()
				.getDrawable(R.drawable.bucket_no_picture));
		bigPicDisplayConfig.setLoadingDrawable(context.getResources()
				.getDrawable(R.drawable.bucket_no_picture));
	}

	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public Object getItem(int position) {
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.shop_grid_item_view, null);
			holder.iv = (ImageView) convertView.findViewById(R.id.item_iv);
			holder.tv = (TextView) convertView.findViewById(R.id.item_tv);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		bitmapUtils.display(holder.iv, items.get(position)
				.getProductTypeImgUrl(), bigPicDisplayConfig);
		holder.tv.setText(items.get(position).getTypename());
		return convertView;
	}

	private class ViewHolder {
		ImageView iv;
		TextView tv;
	}

	public void setDataChange(List<ShopType> list) {
		this.items = list;
		notifyDataSetChanged();
	}

}
